// pages/storeInfo/storeInfo.js
import { getUserOfenShopId } from '../../http/api model/shop'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mapHeight:'500rpx',
    shouqiMaptext:'收起地图',
    msg:"",
    myAdress:{
      longitude:"117.023149",
      latitude:"25.026979"
    },
    markers:[],
    filterMarkers:[],
    loading:true,
    oftenShop:[]
  },
  getOfenSHOP(){
    const id = wx.getStorageSync('openID');
    getUserOfenShopId(id).then(res=>{
      this.setData({
        oftenShop:res
      })
    })
  },
  onLoad(){
    this.getOfenSHOP();
    wx.showLoading({
      title:"加载店铺ing"
    })
    new Promise((resolve,reject)=>{
      this.loadShopMapData()
    }).then(()=>{
      // console.log("========触发导航========");
 // 两地坐标距离测试
//  console.log('两地坐标距离测试',this.getLocation(25.04959,117.027549,25.02959,117.027549));
 wx.getLocation({ //获取当前经纬度
   type: 'wgs84', //返回可以用于wx.openLocation的经纬度，
   success: function (res) {
    //  console.log("触发导航");
    //  console.log("当前地址",res.latitude,res.longitude);
     this.setData({
       ['myAdress.longitude'] : res.latitude,
      ['myAdress.latitude'] : res.longitude,
     })
     for(let index in this.data.markers){
       this.setData({
         ['markers[index].distance']:this.getLocation(
           myAdress.longitude,
           myAdress.latitude,
           item.longitude,
           item.latitude
         )
       })
     }
   },fail(res){
     this.setData({
       msg:res
     })
     wx.showModal({
       cancelColor: 'cancelColor',
       title:"需要授权",
       content:"需要授权位置信息，点击去授权",
       confirmText:"去授权",
       success(res){
         if(res.confirm)wx.openSetting({
           withSubscriptions: true,
         })
       }
     })
   }
 })
    })
   
  },
  loadShopMapData(){
    wx.request({
      url: 'http://localhost:5555/shop/shopMapData',
      method: 'get',
      success: (res) => {
        this.setData({
          markers:res.data,
          filterMarkers:res.data.filter(n=>{
            return this.data.oftenShop.includes(""+n.id)
          })
        })
        
        // myAdress:{
        //   longitude:"117.023149",
        //   latitude:"25.026979"
        // },
        let {markers,myAdress} = this.data
        // console.log('经度',this.data.myAdress.longitude)
        // console.log('纬度',this.data.myAdress.latitude);
        // console.log(this.data.markers[0].latitude);
        for(let item of markers){
          item.distance = this.getLocation(
            myAdress.longitude,
            myAdress.latitude,
            item.longitude,
            item.latitude
          )
          markers.sort((a,b)=>{
            return parseInt(a.distance)-parseInt(b.distance)
          })
          // console.log('地址',markers);
          if(markers.length>=1){
            const myAdress = {
              longitude:markers[0].longitude,
              latitude:markers[0].latitude
            }
            this.setData({
              myAdress
            })
          }
          setTimeout(()=>{
            this.setData({
              markers,
              filterMarkers:markers.filter(n=>{
                return this.data.oftenShop.includes(""+n.id)
              })
            })
          },100)
          setTimeout(()=>{
            this.setData({
              loading:false
            })
          },400)
        }
        wx.hideLoading()
      },
      fail:()=>{
        this.setData({
          list: [{
            imgSrc: "../../images/暂无数据提示.png"
          }]
        })
        wx.hideLoading()
      }
    })
  },
  intoStore(e){
    wx.navigateTo({
      url:`/pages/storeMain/shoreMain?ID=${e.currentTarget.dataset.id}&km=${e.currentTarget.dataset.km}`
    })
  },
  shouqiMap(){
    if(this.data.mapHeight =='500rpx'){
      this.setData({
        mapHeight:'0rpx',
        shouqiMaptext:'打开地图',
        transform:'rotate(225deg)'
      })
    }else{
      this.setData({
        mapHeight:'500rpx',
        shouqiMaptext:'收起地图',
        transform:'rotate(45deg)'
      })
    }

  },
  daohang(event){
    wx.getLocation({ //获取当前经纬度
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，
      success: function (res) {
        wx.setStorageSync('latitude', res.latitude)
       wx.setStorageSync('longitude', res.longitude)
        wx.openLocation({ //使用微信内置地图查看位置。
          latitude: event.currentTarget.dataset.marker.latitude, //要去的纬度-地址
          longitude: event.currentTarget.dataset.marker.longitude, //要去的经度-地址
          name: event.currentTarget.dataset.marker.name,
          address: event.currentTarget.dataset.marker.address
        })
      },fail(res){
        wx.showModal({
          cancelColor: 'cancelColor',
          title:"需要授权",
          content:"需要授权位置信息，点击去授权",
          confirmText:"去授权",
          success(res){
            if(res.confirm)wx.openSetting({
              withSubscriptions: true,
            })
          }
        })
      }
    })
  },
    //拨打电话
    callStorePhone(e) {
      wx.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.phone
      })
    },
    // 计算距离函数
  Rad(d) { 
    //根据经纬度判断距离
    return d * Math.PI / 180.0;
  },
  getLocation(weidu1, jingdu1, weidu2, jingdu2) {
    // console.log(weidu1, jingdu1 ,'我是当前定位', weidu2, jingdu2,'我是目标的');
      // weidu1当前的纬度
      // jingdu1当前的经度
      // weidu2目标的纬度
      // jingdu2目标的经度
      var radweidu1 = this.Rad(weidu1);
      var radweidu2 = this.Rad(weidu2);
      var a = radweidu1 - radweidu2;
      // console.log(a);
      var b = this.Rad(jingdu1) - this.Rad(jingdu2);
      // console.log(b);
      var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radweidu1) * Math.cos(radweidu2) * Math.pow(Math.sin(b / 2), 2)));
      // console.log(s);
      s = s * 6378.137;
      s = Math.round(s * 10000) / 10000;
      s = s.toFixed(1) + 'km' //保留两位小数
      // console.log('经纬度计算的距离:' + s)
      return s
  },
})